

#include "stdint.h"
#include "gd32f30x_fmc.h"
#include "flash_access.h"



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void flash_data_erase(uint32_t page_address, uint16_t page_num)
{
	
    uint32_t erase_counter;

    fmc_unlock();   

    fmc_flag_clear(FMC_FLAG_BANK0_END);     
    fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
    fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    
    for(erase_counter = 0; erase_counter < page_num; erase_counter++){     
        fmc_page_erase(page_address + (SIZE_FLASH_PAGE_SECTOR * erase_counter));
        fmc_flag_clear(FMC_FLAG_BANK0_END);
        fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
        fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    }

    fmc_lock(); 
	
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/


void flash_data_write(uint32_t start_address, uint16_t *data_buf, uint16_t data_num)
{

	fmc_unlock();
	
	for(uint16_t i=0;i<data_num;i++)
	{
		fmc_halfword_program(start_address+i*2, data_buf[i]);
		fmc_flag_clear(FMC_FLAG_BANK0_END);
		fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
		fmc_flag_clear(FMC_FLAG_BANK0_PGERR); 
	}
	
	fmc_lock();
	
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void flash_data_read(uint32_t start_address, uint16_t *data_buf, uint16_t data_num)
{
	uint16_t *param_address = (uint16_t *)start_address;
	uint16_t *data_address  = (uint16_t *)data_buf;
	
	for(uint16_t i=0;i<data_num;i++)
	{
		*(data_address+i) = *(param_address+i);	
	}
	
	
}




